<?php

/**
 * Created by PhpStorm.
 * User: zcw 812039610@qq.com
 * Date: 2019/7/9
 * Time: 11:30
 */

namespace scripts;

use globalunit\model\AdvModel;
use globalunit\utils\GenID;
use globalunit\utils\QueueHelper;
use globalunit\utils\Tools;
use QYS\Protocol\Response;
use QYS\Protocol\Request;
use QYS\Db\Mysql;
use QYS\Db\Mongo;
use QYS\Db\Redis;
use QYS\Log\Log;
use globalunit\utils\Config;
use QYS\Core\Config  as CoreConfig;
use globalunit\model\Web3DModel;

class CMDGATE
{

    public static function gate($request, $response, $rediscli)
    {
        $tcp = [
            "login" => ["ip" =>"", "port" => 0],
            "game" => ["ip" => "", "port" => 0],
        ];
        $gate = CoreConfig::get("gate");
        $debug = CoreConfig::get("debug");

        if ($debug){
            $ws = [
                "login" => "ws://".$gate."/login",
                "game" => "ws://".$gate."/game",
            ];
            return ["errcode" => 0, "data" => ["tcp" => $tcp, "ws" => $ws]];
        }else{
            $ws = [
                "login" => "wss://".$gate."/login",
                "game" => "wss://".$gate."/game",
            ];
            return ["errcode" => 0, "data" => ["tcp" => $tcp, "ws" => $ws]];
        }
    }
}

class GateCtrl
{

    public function invoke($request, $response, $params)
    {
        $response->addHeader("Content-Type", 'application/json');
        $response->addHeader("Access-Control-Allow-Origin", "*");
        $response->addHeader("Access-Control-Allow-Methods", 'POST, GET, OPTIONS, DELETE');
        $response->addHeader('Access-Control-Allow-Headers', "Origin, X-Requested-With, Content-Type, Accept");
        $response->sendHttpHeader();
        $fname = $params['fname'];
        $rediscli = Redis::getInstance("redis1");
        $result = null;
        $result = CMDGATE::$fname($request, $response, $rediscli);
        if (isset($result['errcode']) && $result['errcode'] != 0) {
            $errcode = $result['errcode'];
            $result['errmsg'] = Config::get('errmsg', 'e' . $errcode);
        }
        Log::info('debug', $request, ['fname' => $fname, 'result' => $result]);
        $response->say(json_encode($result));
    }
}
